package com.caesar.utils;

import com.alibaba.druid.pool.DruidDataSource;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * @author caesar
 * @date 2025/7/30
 * @Description:
 * @apiNote
 */
public class JDBCUtilOld {

    private static DruidDataSource dataSource = null;

    static {
        dataSource = new DruidDataSource();
        //设置驱动类的全类名
        dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
        //设置连接数据库的url
        dataSource.setUrl("jdbc:mysql://localhost:3306/test");
        //设置连接数据库的用户名
        dataSource.setUsername("root");
        //设置连接数据库的密码
        dataSource.setPassword("123456");
    }

    public static Connection getConnection() {
        Connection connection = null;
        try {
            connection =  dataSource.getConnection();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return connection;
    }

    public static void close(Connection connection, PreparedStatement statement, ResultSet resultSet) {
        if(resultSet != null) {
            try {
                resultSet.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if(statement != null) {
            try {
                statement.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if(connection != null) {
            try {
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

}

